package com.cskaoyan.shopping.converter;

import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Tangyin
 * @description
 * @since 2022/05/19 20:37
 */
@Component
public class ProductDetailConverter {

    @Autowired
    ItemDescMapper itemDescMapper;

    public ProductDetailDto productDoToProductDto(Item item) {
        ProductDetailDto productDetailDto = new ProductDetailDto();
        productDetailDto.setProductId(item.getId());
        productDetailDto.setSalePrice(item.getPrice());
        productDetailDto.setProductName(item.getTitle());
        productDetailDto.setSubTitle(item.getSellPoint());
        productDetailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
        productDetailDto.setProductImageBig(item.getImageBig());

        Example example = new Example(ItemDesc.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", item.getId());
        List<ItemDesc> itemDescs = itemDescMapper.selectByExample(example);
        ItemDesc itemDesc = itemDescs.get(0);
        productDetailDto.setDetail(itemDesc.getItemDesc());
        String[] arr = item.getImages();
        ArrayList<String> list = new ArrayList<>(Arrays.asList(arr));
        productDetailDto.setProductImageSmall(list);
        return productDetailDto;
    }
}
